﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using BookTransactionControllers.Connection;
using BookTransactionControllers.Services_Impl;
using BookTransactionModels;
using System.Net;

public partial class Views_BookDetail : System.Web.UI.Page
{
    int IDUser1;
    protected void Page_Load(object sender, EventArgs e)
    {
        String bookID = Request.QueryString["ID"].ToString();
        SqlCommand command = new SqlCommand();
        command.CommandText = "Select [Title],[Author],[FullName],[UserID],[CategoryName],[Description],[PublicationYear],[PurposeOfSelling],[SellingPrice],[Image] From [Category],[Book],[User] Where [Category].ID=[Book].CategoryID And [User].ID=[Book].UserID And [Book].ID=@BookID";
        command.Parameters.AddWithValue("@BookID", Request.QueryString["ID"].ToString());
        DataTable tblBookDetail = Connect.ExcuteQuery(command, "BookDetail");
        imgBook.ImageUrl = "../Images/"+tblBookDetail.Rows[0]["Image"].ToString();
        lbName.Text = tblBookDetail.Rows[0]["Title"].ToString();
        lbUser.Text = "Owner: " + tblBookDetail.Rows[0]["FullName"].ToString();
        lbAuthor.Text = "Author: " + tblBookDetail.Rows[0]["Author"].ToString();
        lbCategory.Text = "Category: " + tblBookDetail.Rows[0]["CategoryName"].ToString();
        lbPublicationYear.Text = "Public Year: " + tblBookDetail.Rows[0]["PublicationYear"].ToString();
        lbPurposeOfSelling.Text = "Purpose Of Selling: " + tblBookDetail.Rows[0]["PurposeOfSelling"].ToString();
        lbSellingPrice.Text = "Price: " + tblBookDetail.Rows[0]["SellingPrice"].ToString() + " (VNĐ)";
        lbDescription.Text = "Description: "+ tblBookDetail.Rows[0]["Description"].ToString();
        IDUser1 = Int32.Parse(tblBookDetail.Rows[0]["UserID"].ToString());
    }
    protected void btnSendRequest_Click(object sender, EventArgs e)
    {
        DiaryTransactionManagerImpl diaryTransactionMngIpl = new DiaryTransactionManagerImpl();
        int IDBook1 = Int32.Parse(Request.QueryString["ID"].ToString());
        DiaryTransactionBean diaryTransactionBean = new DiaryTransactionBean();
        diaryTransactionBean.IDBook1 = IDBook1;
        diaryTransactionBean.IDUser1 = IDUser1;
        diaryTransactionBean.IDUser2 = Int32.Parse(Session["UserID"].ToString());
        diaryTransactionBean.Message = txtMessage.Text;
        if (chkTransaction.Checked)
        {
            diaryTransactionBean.IDBook2 = Int32.Parse(lbMybook.SelectedValue);
            diaryTransactionBean.Money = long.Parse(txtMoney.Text);
        }
        else
        {
            diaryTransactionBean.IDBook2 = 0;
            diaryTransactionBean.Money = 0;
        }
        bool ok = diaryTransactionMngIpl.Insert(diaryTransactionBean);
        if (ok)
        {
            Response.Write("Insert OK");
        }
    }
    protected void chkTransaction_CheckedChanged(object sender, EventArgs e)
    {
        if (chkTransaction.Checked)
        {
            lbMybook.Visible = true;
            lbMoney.Visible = true;
            txtMoney.Visible = true;
        }
        else
        {
            lbMybook.Visible = false;
            lbMoney.Visible = false;
            txtMoney.Visible = false;
        }
    }
    protected void rdbtnlstRate_SelectedIndexChanged(object sender, EventArgs e)
    {
        String BookID = Request.QueryString["ID"].ToString();
        String domain = Dns.GetHostName();
        IPAddress[] ipAdrr = Dns.GetHostAddresses(domain);
        //String ip = Session["IpVote"].ToString();
        //String book = Session["BookID"].ToString();
        bool checkIp = Session["IpVote"] !=null;
        bool checkBook = Session["BookID"].ToString().Equals(BookID);
        if (checkIp && checkBook)
	    {
            lbThank.Text = "You voted!";
	    }
        else
	    {
            int Value = Int32.Parse(rdbtnlstRate.SelectedValue);
            VoteBean vote = new VoteBean();
            vote.BookID = BookID;
            vote.Value = Value;
            VoteManagerImpl voteMngImpl = new VoteManagerImpl();
            voteMngImpl.ExcuteVoteBook(vote);
            lbThank.Text = "Thank your vote!";
            Session.Add("IpVote", ipAdrr[ipAdrr.Length - 1].ToString());
            Session.Add("BookID", BookID);
	    }
    }
}